package cn.spike.system.controller;

import cn.spike.basic.util.AjaxResult;
import cn.spike.basic.util.PageList;
import cn.spike.system.domain.SystemDictionaryType;
import cn.spike.system.query.SystemDictionaryTypeQuery;
import cn.spike.system.service.SystemDictionaryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author:spike
 * @create:2023-02-01 22:01
 */
@Api(value = "系统管理数据字典类型的API",description = "处理系统管理数据字典类型的操作")
@RestController
@RequestMapping("/systemdictionarytype")
public class SystemDictionaryTypeController {
    @Autowired
    private SystemDictionaryTypeService systemDictionaryTypeService;

    /**
     * 接口：查询单个数据
     * @param id
     * @return
     */
    //前端：GET localhost:8080/systemDictionaryType/1
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个数据")
    public SystemDictionaryType loadOne(@PathVariable("id")Long id){
        return systemDictionaryTypeService.loadById(id);
    }

    /**
     * 接口：查询所有
     * @return
     */
    //前端：GET localhost:8080/systemDictionaryType
    @GetMapping
    @ApiOperation(value = "查询所有数据")
    public List<SystemDictionaryType> loadAll(){
        return systemDictionaryTypeService.loadAll();
    }

    /**
     * 接口：删除单条数据
     * @param id
     * @return
     */
    //前端：DELETE http://localhost:8080/systemDictionaryType/1
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单条数据")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            systemDictionaryTypeService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 接口：添加或修改
     * @param systemDictionaryType
     * @return
     */
    //前端：PUT http://localhost:8080/systemDictionaryType json对象
    @PutMapping
    @ApiOperation(value = "添加或修改")
    public AjaxResult save(@RequestBody SystemDictionaryType systemDictionaryType){
        try {
            if(systemDictionaryType.getId()==null){
                systemDictionaryTypeService.add(systemDictionaryType);
            }else {
                systemDictionaryTypeService.update(systemDictionaryType);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 接口：分页和高级查询
     * @param systemDictionaryTypeQuery
     * @return
     */
    //前端：POST http://localhost:8080/systemDictionaryType json对象
    @PostMapping
    @ApiOperation(value = "分页和高级查询")
    public PageList<SystemDictionaryType> pageQuery(@RequestBody SystemDictionaryTypeQuery systemDictionaryTypeQuery){
        return systemDictionaryTypeService.pageQuery(systemDictionaryTypeQuery);
    }

    /**
     * 接口：批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            systemDictionaryTypeService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }
}
